package org.aplombh.java.interview.newcoder;

import java.util.Arrays;
import java.util.HashMap;

public class NC61_两数之和 {

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param numbers int整型一维数组
     * @param target  int整型
     * @return int整型一维数组
     */
    public static int[] twoSum(int[] numbers, int target) {

        HashMap<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];

        for (int i = 0; i < numbers.length; i++) {
            int ans = target - numbers[i];
            if (map.containsKey(ans)) {
                if (map.get(ans) <= numbers[i]) {
                    res[0] = map.get(ans) + 1;
                    res[1] = i + 1;
                } else {
                    res[1] = map.get(ans) + 1;
                    res[0] = i + 1;
                }
                break;
            }
            map.put(numbers[i], i);
        }
        return res;
    }


    public static void main(String[] args) {
        int[] ints = twoSum(new int[]{0,4,3,0}, 0);
        System.out.println(Arrays.toString(ints));
    }
}
